package com.sync.mutiParse;

import java.nio.ByteBuffer;

import com.constants.Base;
import com.constants.Column;

/**
 * 队列预读
 * 
 * @author Administrator
 *
 */
public class GoodCodeUtils extends Base {

	//// first_name:2:0|NULL|徐|last_name:2:0|NULL|依|sex:2:0|NULL|男|score:1:0|NULL|66|score1:1:0|NULL|66
	// score1有可能存在有可能不存在
	public static void handleInsert(byte[] src, int srcStart,
			ByteBuffer middleBuffer, int startPos, int id) {
		int position = srcStart;
		byte tmpByte;
		int newPosition = startPos - Column.chunk;
		for (int i = 0; i < Column.count; i++) {
			position += Column.COL$_LEN[i] + Column.NULL$_LEN;

			newPosition += Column.chunk;
			middleBuffer.position(newPosition);
			while ((tmpByte = src[position++]) != SEP) {
				middleBuffer.put(tmpByte);
			}
			middleBuffer.put(SEP);
		}
		newPosition += Column.chunk;
		middleBuffer.position(newPosition);
	}

	// first_name:2:0|林|郑| 也有可能直接是一个换行符号（仅仅有主见变更）
	public static void handleUpdate(byte[] src, int srcStart,
			ByteBuffer middleBuffer, int startPos, int id) {
		int position = srcStart;

		byte tmpByte;
		int newPosition;
		while ((tmpByte = src[position]) != SEPLINE) {
			newPosition = startPos;
			if (tmpByte == 102) {// first_name:1:0| 102
				position += Column.COL$_LEN[0];
			} else if (tmpByte == 108) {// last_name:1:0|
										// 108
				position += Column.COL$_LEN[1];
				newPosition += Column.chunk;
			} else {
				if (src[position + 1] == 101) {// sex:1:0|
												// 105
												// 101
					position += Column.COL$_LEN[2];
					newPosition += Column.chunk + Column.chunk;
				} else {

					if (src[position + Column.COL_LEN[3]] == SEP2) {// score:1:0|
						position += Column.COL$_LEN[3];
						newPosition += Column.chunk + Column.chunk
								+ Column.chunk;
					} else if (src[position + Column.COL_LEN[4]] == SEP2) {// score2:1:0|
						position += Column.COL$_LEN[4];
						newPosition += Column.chunk + Column.chunk
								+ Column.chunk + Column.chunk;
					} else {
						throw new RuntimeException();
					}
				}
			}

			while (src[position++] != SEP) {
			} // 旧值跳过
			middleBuffer.position(newPosition);

			while ((tmpByte = src[position++]) != SEP) {// 读取新值
				middleBuffer.put(tmpByte);
			}
			middleBuffer.put(SEP);
		}

	}

}
